package com.franzliszt.maillist.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class Dao {
    public static final String TAG = "DataBase";
    private Helper helper ;
    private SQLiteDatabase DB;
    public Dao(Context context)
    {
        helper = new Helper(context);
    }
    public void Insert(News news){
        DB = helper.getReadableDatabase();
        if (DB.isOpen())
        {
            ContentValues values = new ContentValues();
            values.put( Helper.Row_Phone,news.getPhoneNumber());
            values.put( Helper.Row_LastName,news.getLastName());
            values.put( Helper.Row_FirstName,news.getFirstName());
            values.put(Helper.Row_PinYin,news.getPinYin());
            values.put( Helper.Row_School,news.getSchool());
            values.put( Helper.Row_Address,news.getAddress());
            values.put(Helper.Row_Other,news.getOther());
            long RowId = DB.insert(Helper.TableName,null,values);
            if(RowId == -1)
                Log.i(TAG, "数据插入失败！");
            else
                Log.i(TAG, "数据插入成功！"+RowId);
        }
    }
    public void Delete(String Phone){
        DB = helper.getReadableDatabase();
        if (DB.isOpen()){
            String whereClause = "PhoneNumber = ?";
            String[] whereArgs = {Phone + ""};
            int count = DB.delete(Helper.TableName, whereClause, whereArgs);
            if (count > 0)
                Log.i(TAG, "删除了: " + count + "行");
            else
                Log.i(TAG, "数据未删除！");
            DB.close(); // 数据库关闭
        }
    }
    public void DeleteAll(){
        DB = helper.getReadableDatabase();
        if (DB.isOpen()){
            String whereClause = null;
            String[] whereArgs = null;
            int count = DB.delete(Helper.TableName, whereClause, whereArgs);
            if (count > 0)
                Log.i(TAG, "删除了: " + count + "行");
            else
                Log.i(TAG, "数据未删除！");
            DB.close(); // 数据库关闭
        }
    }
    public void Update(String PhoneNumber,String School,String Addr,String Other){
        DB = helper.getReadableDatabase();
        if (DB.isOpen()){
            ContentValues values = new ContentValues();
            values.put( Helper.Row_School,School);
            values.put( Helper.Row_Address,Addr);
            values.put(Helper.Row_Other,Other);
            int count = DB.update(Helper.TableName,values,"PhoneNumber = ?",new String[]{PhoneNumber});
            if (count > 0){
                Log.d(TAG,"update password row="+count);
            }else {
                Log.d(TAG,"update fail");
            }
            DB.close();
        }
    }
    public List<News> QueryAll() {
        DB = helper.getReadableDatabase();  // 获得一个只读的数据库对象
        if(DB.isOpen()) {
            String[] columns = {Helper.Row_Phone, Helper.Row_LastName,Helper.Row_FirstName,Helper.Row_PinYin,Helper.Row_School,Helper.Row_Address,Helper.Row_Other};  // 需要的列
            String selection = null;    // 选择条件, 给null查询所有
            String[] selectionArgs = null;  // 选择条件的参数, 会把选择条件中的? 替换成数据中的值
            String groupBy = null;  // 分组语句  group by name
            String having = null;   // 过滤语句
            String orderBy = null;  // 排序
            Cursor cursor = DB.query(Helper.TableName, columns, selection, selectionArgs, groupBy, having, orderBy);
            String last;
            String first;
            String pinyin;
            String phone;
            String school;
            String addr;
            String other;

            if(cursor != null && cursor.getCount() > 0) {
                List<News> news = new ArrayList<News>();
                while(cursor.moveToNext()) {
                    last = cursor.getString(cursor.getColumnIndex( Helper.Row_LastName ));
                    first = cursor.getString(cursor.getColumnIndex( Helper.Row_FirstName ));
                    phone = cursor.getString(cursor.getColumnIndex( Helper.Row_Phone));
                    pinyin = cursor.getString(cursor.getColumnIndex( Helper.Row_PinYin));
                    school = cursor.getString(cursor.getColumnIndex(Helper.Row_School));
                    addr = cursor.getString(cursor.getColumnIndex( Helper.Row_Address ));
                    other = cursor.getString( cursor.getColumnIndex( Helper.Row_Other ) );
                    news.add(new News(last,first,pinyin,phone,school,addr,other));
                }
                DB.close();
                return news;
            }
            if (cursor != null) {
                cursor.close();
            }
            DB.close();
        }
        return null;
    }
}
